
#include "app_config.h"
#include "config.h"
#include "debug.h"

#include "STC32G_Timer.h"
#include "STC32G_GPIO.h"
#include "STC32G_NVIC.h"
#include "STC32G_Exti.h"
#include "STC32G_Delay.h"
#include "STC32G_Switch.h"
#include "STC32G_Compare.h"
#include "STC32G_ADC.h"
#include "math.h"

#include "drv_adc_sensors.h"



#if (TCFG_DRV_ADC_NTC_SENSOR_SUPPORT || TCFG_DRV_ADC_VOL_SENSOR_SUPPORT || TCFG_DRV_ADC_UV_SENSOR_SUPOORT)
/**
 * @brief   ADC初始化
*/
void drv_adc_sensors_init(void)
{
    ADC_InitTypeDef		ADC_InitStructure;		//结构定义

#if TCFG_DRV_ADC_NTC_SENSOR_SUPPORT
    P0_MODE_IN_HIZ(GPIO_Pin_6);	//P0.6 设置为高阻输入
#endif

#if TCFG_DRV_ADC_VOL_SENSOR_SUPPORT
    
#endif

#if TCFG_DRV_ADC_UV_SENSOR_SUPOORT
    // P0_MODE_IN_HIZ(GPIO_Pin_LOW | GPIO_Pin_5);	//P0.0~P0.6 设置为高阻输入
    P0_MODE_IN_HIZ(GPIO_Pin_5);	//P0.5 设置为高阻输入
#endif

    ADC_InitStructure.ADC_SMPduty   = 31;		//ADC 模拟信号采样时间控制, 0~31（注意： SMPDUTY 一定不能设置小于 10）
    ADC_InitStructure.ADC_CsSetup   = 0;		//ADC 通道选择时间控制 0(默认),1
    ADC_InitStructure.ADC_CsHold    = 1;		//ADC 通道选择保持时间控制 0,1(默认),2,3
    ADC_InitStructure.ADC_Speed     = ADC_SPEED_2X16T;		//设置 ADC 工作时钟频率	ADC_SPEED_2X1T~ADC_SPEED_2X16T
    ADC_InitStructure.ADC_AdjResult = ADC_RIGHT_JUSTIFIED;	//ADC结果调整,	ADC_LEFT_JUSTIFIED,ADC_RIGHT_JUSTIFIED
    ADC_Inilize(&ADC_InitStructure);		//初始化
    ADC_PowerControl(ENABLE);				//ADC电源开关, ENABLE或DISABLE
    NVIC_ADC_Init(DISABLE,Priority_0);		//中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3 
}
#endif


#if TCFG_DRV_ADC_NTC_SENSOR_SUPPORT
float drv_ntc_tempertaure_get(void)
{
	#define _R2 10000		/* NTC串接到GND的电阻R2阻值 */
	float Rp=10000;			/* NTC 阻值 */ 
	float Bx=3950;			/* NTC B值  */
	float T2=273.15+25;
	float Ka=273.15;		/* K值 开尔文 */
	float Rt=0;
	float vol=0;
	float Tempertaure=0;

	uint16 adc_value = 0;	

	// 读取ADC值
	
	adc_value = (uint16)Get_ADCResult(ADC_CH14);

	/* 通过ADC值计算温度值 */ 	
   	vol=(float)(adc_value*(3.3/4095));
   	Rt=(3.3-vol)*_R2/vol;

   	Tempertaure=(float)(1/(1/T2+log(Rt/Rp)/Bx)-Ka+0.5);

	// log_d("AIN4 adc_value:%u, vol:%.02fV, T:%.02f°C\r\n", adc_value, vol, Tempertaure); 

	return Tempertaure;
}
#endif


#if TCFG_DRV_ADC_VOL_SENSOR_SUPPORT

#endif

#if TCFG_DRV_ADC_LT_SENSOR_SUPPORT
uint16_t LDR_LuxData(uint16_t adcval)
{
	float voltage = 0;	
	float R = 0;	
	uint16_t Lux = 0;
	voltage = adcval; //LDR_Average_Data();
	voltage  = voltage / 4096 * 3.3f;
	
	R = voltage / (3.3f - voltage) * 10000;
		
	Lux = 40000 * pow(R, -0.6021);
	
	if (Lux > 999)
	{
		Lux = 999;
	}
	return Lux;
}
#endif

#if TCFG_DRV_ADC_UV_SENSOR_SUPOORT
#define UV_LEVEL_00_VAL     (50)     // 紫外线等级0
#define UV_LEVEL_01_VAL     (227)    // 紫外线等级1
#define UV_LEVEL_02_VAL     (318)    // 紫外线等级2
#define UV_LEVEL_03_VAL     (408)    // 紫外线等级3
#define UV_LEVEL_04_VAL     (503)    // 紫外线等级4
#define UV_LEVEL_05_VAL     (606)    // 紫外线等级5
#define UV_LEVEL_06_VAL     (696)    // 紫外线等级6
#define UV_LEVEL_07_VAL     (795)    // 紫外线等级7
#define UV_LEVEL_08_VAL     (881)    // 紫外线等级8
#define UV_LEVEL_09_VAL     (976)    // 紫外线等级9
#define UV_LEVEL_10_VAL     (1079)   // 紫外线等级10
#define UV_LEVEL_11_VAL     (1170)   // 紫外线等级11

/**
 * @brief   读取UV sensor紫外线强读索引
 * @param   无
 * @return  紫外线强度0~11
*/
uint8_t drv_adc_sensors_uv_level_read(void)
{
    uint8_t i=0;
    uint8_t uv_index=0;
    uint16_t val=0;
    for (i=0; i<5; i++) {
        val += Get_ADCResult(ADC_CH13);
    }
    val /= 5;

    log_d("uv adc:%d\n", val);

    if (val < UV_LEVEL_00_VAL) {
        uv_index = 0;
    } else if (val < UV_LEVEL_01_VAL) {
        uv_index = 1;
    } else if (val < UV_LEVEL_02_VAL) {
        uv_index = 2;
    } else if (val < UV_LEVEL_03_VAL) {
        uv_index = 3;
    } else if (val < UV_LEVEL_04_VAL) {
        uv_index = 4;
    } else if (val < UV_LEVEL_05_VAL) {
        uv_index = 5;
    } else if (val < UV_LEVEL_06_VAL) {
        uv_index = 6;
    } else if (val < UV_LEVEL_07_VAL) {
        uv_index = 7;
    } else if (val < UV_LEVEL_08_VAL) {
        uv_index = 8;
    } else if (val < UV_LEVEL_09_VAL) {
        uv_index = 8;
    } else if (val < UV_LEVEL_10_VAL) {
        uv_index = 10;
    } else if (val >= UV_LEVEL_11_VAL) {
        uv_index = 11;
    }

    return uv_index;
}
#endif



